using System.Collections.Generic;
using System;
using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using System.Linq;
using Newtonsoft.Json;

using Read.Services;
using Read.Models;

namespace Read.Web.Admin
{
    /// <summary>
    /// 后台管理
    /// </summary>
    //[Authorize]
    public class HomeController : AdminBaseController
    {
        /// <summary>
        /// 后台首页 //throw new InvalidOperationException("You did something invalid.");
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            //var role = User.FindFirst(ClaimTypes.Role).Value;
            //var userData = User.FindFirst(ClaimTypes.UserData).Value;
            //var userName = User.Identity.Name;
            ViewBag.userName = IdentityName;
            //return Json(new { Role = role, UserName = userName });
            //var menu = new NodeServices();
            //var menulist = menu.getMenu(role);  //JsonConvert.SerializeObject(adminlist);
            //ViewBag.UserData = JsonConvert.SerializeObject(menulist);// + Connection.MySql;
            //return Json(JsonConvert.DeserializeObject<ApplicationUser>(userData));
            return View();
        }

        /// <summary>
        /// 默认欢迎页
        /// </summary>
        /// <returns></returns>
        public IActionResult Main()
        {
            return View();
        }

        //---------------以下都是调试信息忽略不用管----------------//
        public IActionResult Test3()
        {
            var t = new NodeService();
            //Console.WriteLine(t.getAccessTree(2));
            return Json(t.getNodeList(2));
            //return View();
        }
        /// <summary>
        /// Test2
        /// </summary>
        /// <returns></returns>
        public IActionResult Test2()
        {
            List<department> ds = new List<department>()
            {
                new department(){ID=1,Name="中国移动",PID=0},
                new department(){ID=2,Name="中国人寿",PID=0},
                new department(){ID=3,Name="销售公司",PID=1},
                new department(){ID=4,Name="生成公司",PID=1},
                new department(){ID=5,Name="销售部",PID=3},
                new department(){ID=6,Name="生产部",PID=4},
                new department(){ID=7,Name="网络部",PID=2},
                new department(){ID=8,Name="研发部",PID=2},
                new department(){ID=9,Name="设计部",PID=2},
                new department(){ID=10,Name="销售管理部",PID=3},
            };
            var menu = new NodeService();
            menulist = menu.getNodeList(2);  //JsonConvert.SerializeObject(adminlist);

            ViewBag.str = departmentRecursion(0, menulist);
            return View();
            //return Content(departmentRecursion(0, menulist));
            //return Json(departmentRecursion2(0, menulist));
        }
        public class department
        {
            public int ID { get; set; }
            public int PID { get; set; }
            public string Name { get; set; }
        }
        private MenuModelTest departmentRecursion2(int pid, List<NodeModel> obj)
        {
            MenuModelTest ur = new MenuModelTest();
            MenuModelTest nd = new MenuModelTest();
            //string a = "<dl style='margin:0px;padding:0px;'>";
            for (int i = 0; i < obj.Count; i++)
            {
                if (obj[i].Pid == pid)
                {
                    nd = departmentRecursion2(obj[i].Id, obj);
                    if (ur.id == obj[i].Pid)
                    {
                        ur.children.Add(nd);
                    }
                    //nd.id = obj[i].Id.ToString();
                    //nd.title = flg(obj[i].Level) + obj[i].Title;
                    //a += "<dt  style='border-bottom:1px solid #808080'>" + obj[i].Title + "</dt>\n<dd>" + departmentRecursion2(obj[i].Id, obj) + "</dd>";
                    //ur.id = obj[i].Id.ToString();
                    //ur.title = flg(obj[i].Level) + obj[i].Title;
                    //ur.url = "/Admin/Access/UserList";

                }
            }
            //a += "</dl>";
            //ur.children.Add(nd);
            return ur;
        }
        private string departmentRecursion(int pid, List<NodeModel> obj)
        {
            string a = "<dl>";
            for (int i = 0; i < obj.Count; i++)
            {
                if (obj[i].Pid == pid)
                {
                    if (obj[i].Remark == "true")
                    {
                        a += "<dt><input type='checkbox' name='node[" + obj[i].Id + "]' lay-skin='primary' title='" + obj[i].Title + "' checked ></dt>" +
                        "<dd class='cdd'>" + departmentRecursion(obj[i].Id, obj) + "</dd>";
                    }
                    else
                    {
                        a += "<dt><input type='checkbox' name='node[" + obj[i].Id + "]' lay-skin='primary' title='" + obj[i].Title + "' ></dt>" +
                        "<dd class='cdd'>" + departmentRecursion(obj[i].Id, obj) + "</dd>";
                    }
                }
            }
            a += "</dl>";
            return a;
            //------------------------
            // string a = "<div class='layui-collapse'><div class='layui-colla-item'>";
            // //string str = "";
            // for (int i = 0; i < obj.Count; i++)
            // {
            //     if (obj[i].Pid == pid)
            //     {
            //         if (obj[i].Level < 4 && obj.Where(x => x.Pid == obj[i].Id).Count() > 0)
            //         {
            //             //str += flg(obj[i].Level) + obj[i].Title + "\n" + departmentRecursion(obj[i].Id, obj);
            //             a += "<h2 class='layui-colla-title'><input name='" + obj[i].Id + "' type='checkbox' value='" + obj[i].Id + "' />" + obj[i].Title + obj[i].Level + "</h2><div class='layui-colla-content layui-show'>" + departmentRecursion(obj[i].Id, obj) + "</div>";
            //         }
            //         else
            //         {
            //             a += "<h2 class='layui-colla-title'><input name='" + obj[i].Id + "' type='checkbox' value='" + obj[i].Id + "' />" + obj[i].Title + obj[i].Level + "</h2>";
            //         }
            //     }
            // }
            // a += "</div></div>";
            // return a;
            // //return str + "";
        }

        /// <summary>
        /// Test
        /// </summary>
        /// <returns></returns>
        public IActionResult Test()
        {
            // // var role = User.FindFirst(ClaimTypes.Role).Value;
            // // var userData = User.FindFirst(ClaimTypes.UserData).Value;
            // // var userName = User.Identity.Name;
            // // //return Json(new { Role = role, UserName = userName });
            // // var menu = new NodeServices();
            // // List<MenuModel> menulist = menu.getMenu(role);  //JsonConvert.SerializeObject(adminlist);
            // // return Json(menulist);
            var menu = new NodeService();
            menulist = menu.getNodeList(1);  //JsonConvert.SerializeObject(adminlist);
            //调用  
            //Create(new MenuModelTest(), 0);
            var t = Create(new MenuModelTest(), 1);
            //打印  
            //Print(t);
            return Json(t.children);
        }
        static List<NodeModel> menulist = new List<NodeModel>();
        //测试数据  
        // static List<Users> list = new List<Users>()
        //     {
        //         new Users(){id=1,name="中国移动",parentId=0},
        //         new Users(){id=2,name="中国人寿",parentId=0},
        //         new Users(){id=3,name="销售公司",parentId=1},
        //         new Users(){id=4,name="生成公司",parentId=1},
        //         new Users(){id=5,name="销售部",parentId=3},
        //         new Users(){id=6,name="生产部",parentId=4},
        //         new Users(){id=7,name="网络部",parentId=2},
        //         new Users(){id=8,name="研发部",parentId=2},
        //         new Users(){id=9,name="设计部",parentId=2},
        //         new Users(){id=5,name="销售管理部",parentId=3},
        //     };

        //最终结果  
        //static UserR ur = new UserR();
        //递归遍历  
        private static MenuModelTest Create(MenuModelTest node, int id)
        {
            MenuModelTest ur = new MenuModelTest();
            var q = menulist.Where(x => x.Pid == id).ToList();
            for (int i = 0; i < q.Count; i++)
            {
                MenuModelTest nd = new MenuModelTest();
                nd.id = q[i].Id;
                nd.title = q[i].Title;
                nd.icon = q[i].Icon;
                nd.url = $"/{q[i].App}/{q[i].Controller}/{q[i].Action}";
                nd.spread = true;
                //nd.parentId = q[i].parentId;
                Create(nd, q[i].Id);
                if (id == 1)// id = 0 是根节点
                    ur.children.Add(nd);
                node.children.Add(nd);
            }
            return ur;
        }
        // //递归遍历  
        // private static MenuModelTest Create(MenuModelTest node, int id)
        // {
        //     MenuModelTest ur = new MenuModelTest();
        //     var q = menulist.Where(x => x.Pid == id).ToList();
        //     if (id == 0)                                  // id = 0 是根节点     
        //     {
        //         for (int i = 0; i < q.Count; i++)
        //         {
        //             MenuModelTest nd = new MenuModelTest();
        //             nd.id = q[i].Id.ToString();
        //             nd.title = flg(q[i].Level) + q[i].Title;
        //             nd.icon = "&#xe6c6;";
        //             //nd.parentId = q[i].parentId;
        //             Create(nd, q[i].Id);
        //             ur.children.Add(nd);
        //         }
        //     }
        //     else
        //     {
        //         for (int i = 0; i < q.Count; i++)
        //         {
        //             MenuModelTest Tnode = new MenuModelTest();
        //             Tnode.id = q[i].Id.ToString();
        //             Tnode.title = flg(q[i].Level) + q[i].Title;
        //             Tnode.icon = "&#xe6c6;";
        //             //Tnode.parentId = q[i].parentId;
        //             Create(Tnode, q[i].Id);
        //             node.children.Add(Tnode);
        //         }
        //     }
        //     return ur;
        // }

        //打印查看结果  
        static void Print(MenuModelTest ur)
        {
            //Console.WriteLine("------------------");
            Console.WriteLine(ur.title);
            if (ur.children != null)
                foreach (var item in ur.children)
                {
                    Print(item);
                }
        }

        static string flg(int level)
        {
            string str = string.Empty;
            switch (level)
            {
                case 1:
                    return "=";
                case 2:
                    return "==";
                case 3:
                    return "===";
                default:
                    return "====";
            }
        }
        //查询出的实体层  
        public class Users
        {
            public int id { get; set; }
            public string name { get; set; }
            public int parentId { get; set; }
        }
        //遍历后的实体层  
        public class UserR
        {
            public int id { get; set; }
            public string name { get; set; }
            public List<UserR> u = new List<UserR>();
            public int parentId { get; set; }
        }

        /// <summary>
        /// 动态菜单类
        /// </summary>
        public class MenuModelTest
        {
            /// <summary>
            /// Id
            /// </summary>
            public int id { get; set; }

            /// <summary>
            /// 标题
            /// </summary>
            public string title { get; set; }

            /// <summary>
            ///图标
            /// </summary>
            public string icon { get; set; }

            /// <summary>
            /// 是否展开
            /// </summary>
            public bool spread { get; set; }

            /// <summary>
            ///链接
            /// </summary>
            public string url { get; set; }

            /// <summary>
            /// 子菜单列表
            /// </summary>
            /// <returns></returns>
            public List<MenuModelTest> children = new List<MenuModelTest>();
        }
    }
}